/**
 * Created by ebalmes Oct 7, 2009 1:15:28 PM
 * 
 */
package gui.dialogs;

import exceptions.DataException;
import gui.components.XField;
import gui.validators.DateValidator;
import gui.validators.StringValidator;
import gui.validators.TajValidator;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.DataModel;
import model.types.Patient;

public class CreatePatientDialog extends BasicDialog
{
  public static final String ADDRESS = "address";

  public static final String MOTHERS_NAME = "mothers_name";

  public static final String PLACE_OF_BIRTH = "place_of_birth";

  public static final String DATE_OF_BIRTH = "date_of_birth";

  public static final String TAJNUMBER = "tajnumber";

  public static final String NAME = "name";

  private static Logger logger = Logger.getLogger(CreatePatientDialog.class.getName());
  
  private JLabel nameLabel;
  private JLabel tajLabel;
  private JLabel placeOfBirthLabel;
  private JLabel dateOfBirthLabel;
  private JLabel mothersNameLabel;
  private JLabel addressLabel;

  public CreatePatientDialog(String title, JFrame owner) {
    super(title, owner);
    logger.log(Level.INFO, "Open patient dialog created.");
  }

  @Override
  public void OK() {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    if (validateAllFields()) {
      try {
        String name = fieldMap.get(NAME).getText();
        Integer tajNumber = Integer.parseInt(fieldMap.get(TAJNUMBER).getText());
        Date dateOfBirth;
        dateOfBirth = df.parse(fieldMap.get(DATE_OF_BIRTH).getText());
        String placeOfBirth = fieldMap.get(PLACE_OF_BIRTH).getText();
        String mothersName = fieldMap.get(MOTHERS_NAME).getText();
        String address = fieldMap.get(ADDRESS).getText();
        Patient patient = new Patient(name, tajNumber, mothersName,
            dateOfBirth, placeOfBirth, address);
        DataModel.getInstance().addPatient(patient);
      } catch (DataException e) {
        logger.log(Level.SEVERE, e.getMessage());
        JOptionPane.showMessageDialog(this, e);
        return;
      } catch (ParseException e) {
        logger.log(Level.SEVERE, e.getMessage());
        JOptionPane.showMessageDialog(this, e);
        return;
      }
      setVisible(false);
    }
  }

  @Override
  public void createGUI() {
    
    nameLabel = new JLabel("Name: ");
    tajLabel = new JLabel("Taj-number: ");
    dateOfBirthLabel = new JLabel("Date of birth: ");
    placeOfBirthLabel = new JLabel("Place of birth: ");
    mothersNameLabel = new JLabel("Mother's name: ");
    addressLabel = new JLabel("Address: ");
    fieldMap.put(NAME, new XField(new StringValidator(), 20));
    fieldMap.put(TAJNUMBER,new XField(new TajValidator(), 20));
    fieldMap.put(DATE_OF_BIRTH,new XField(new DateValidator(), 20));
    fieldMap.put(PLACE_OF_BIRTH, new XField(new StringValidator(), 20));
    fieldMap.put(MOTHERS_NAME,new XField(new StringValidator(), 20));
    fieldMap.put(ADDRESS,new XField(new StringValidator(), 20));
   
    setLayout(new BorderLayout());
    add(createFieldsAndLabelsPanel(), BorderLayout.CENTER);
    add(createControlPanel(this), BorderLayout.SOUTH);    
  }
  
  /**
   * Creates the fields and the Labels on the dialog
   */
  private JPanel createFieldsAndLabelsPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    c.insets = new Insets(5, 5, 5, 5);
    panel.add(nameLabel, c);
    c.gridy++;
    panel.add(tajLabel, c);
    c.gridy++;
    panel.add(dateOfBirthLabel, c);
    c.gridy++;
    panel.add(placeOfBirthLabel, c);
    c.gridy++;
    panel.add(mothersNameLabel, c);
    c.gridy++;
    panel.add(addressLabel, c);
    c.gridx = 1;
    c.gridy = 0;
    panel.add(fieldMap.get(NAME), c);
    c.gridy++;
    panel.add(fieldMap.get(TAJNUMBER), c);
    c.gridy++;
    panel.add(fieldMap.get(DATE_OF_BIRTH), c);
    c.gridy++;
    panel.add(fieldMap.get(PLACE_OF_BIRTH), c);
    c.gridy++;
    panel.add(fieldMap.get(MOTHERS_NAME), c);
    c.gridy++;
    panel.add(fieldMap.get(ADDRESS), c);
    return panel;
  }

  @Override
  public void loadContent() {
    // TODO Auto-generated method stub
  }

  @Override
  public void releaseResources() {
    fieldMap.get(NAME).setText("");
    fieldMap.get(TAJNUMBER).setText("");
    fieldMap.get(DATE_OF_BIRTH).setText("");
    fieldMap.get(PLACE_OF_BIRTH).setText("");
    fieldMap.get(MOTHERS_NAME).setText("");
    fieldMap.get(ADDRESS).setText("");
  }

  
}
